// Generated on 2016-03-17 using generator-angular 0.15.1
'use strict';

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var openURL = require('open');
var lazypipe = require('lazypipe');
var rimraf = require('rimraf');
var wiredep = require('wiredep').stream;
var runSequence = require('run-sequence');
var shell = require('gulp-shell');
var jasmine = require('gulp-jasmine');

var yeoman = {
    app: require('./bower.json').appPath || 'wwwroot',
    dist: 'dist'
};

var paths = {
    scripts: [
        yeoman.app + '/js/app.js',
        yeoman.app + '/js/config.js',
        yeoman.app + '/js/config.*.js',
        yeoman.app + '/js/services/*.js',
        yeoman.app + '/js/filters/*.js',
        yeoman.app + '/js/directives/*.js',
        yeoman.app + '/js/controllers/*.js',
        yeoman.app + '/js/main.js',
        yeoman.app + '/js/app/*.js',
    ],
    styles: [yeoman.app + '/css/**/*.css'],
    test: ['test/spec/**/*.js'],
    testRequire: [
      //yeoman.app + '/libs/angular/angular.js',
      //yeoman.app + '/libs/angular-mocks/angular-mocks.js',
      //yeoman.app + '/libs/angular-animate/angular-animate.js',
      //yeoman.app + '/libs/angular-touch/angular-touch.js',
      //yeoman.app + '/libs/angular-ui-grid/ui-grid.js',
      //yeoman.app + '/vendor/angular/ngstorage/ngStorage.js',
      //yeoman.app + '/vendor/angular/ngstorage/ngStorage.js',
      //yeoman.app + '/libs/angular-resource/angular-resource.js',
      //yeoman.app + '/libs/angular-cookies/angular-cookies.js',
      //yeoman.app + '/libs/angular-sanitize/angular-sanitize.js',
      //yeoman.app + '/libs/angular-route/angular-route.js',
      //yeoman.app + '/libs/jquery/dist/jquery.js',
      yeoman.app + '/vendor/angular.js',
      yeoman.app + '/vendor/angular/**/*.js',
      yeoman.app + '/vendor/jquery/jquery.min.js',
      yeoman.app + '/vendor/libs/**/*.js',
      yeoman.app + '/vendor/modules/**/*.min.js',
      yeoman.app + '/libs/angular-ui-grid/ui-grid.js',
      yeoman.app + '/libs/mustache.js/mustache.js',
      'test/mock/**/*.js'
    ],
    karma: 'test/karma.conf.js',
    karma_dnx: 'test/karma.conf.dnx.js',
    views: {
        main: yeoman.app + '/index.html',
        files: [yeoman.app + '/tpl/**/*.html'],
    }
};

////////////////////////
// Reusable pipelines //
////////////////////////

var lintScripts = lazypipe()
  .pipe($.jshint, '.jshintrc')
  .pipe($.jshint.reporter, 'jshint-stylish');

var styles = lazypipe()
  .pipe($.autoprefixer, 'last 1 version')
  .pipe(gulp.dest, '.tmp/styles');

///////////
// Tasks //
///////////

gulp.task('styles', function () {
    return gulp.src(paths.styles)
      .pipe(styles());
});

gulp.task('lint:scripts', function () {
    return gulp.src(paths.scripts)
      .pipe(lintScripts());
});

gulp.task('clean:tmp', function (cb) {
    rimraf('./.tmp', cb);
});

gulp.task('start:client', ['start:server', 'styles'], function () {
    openURL('http://localhost:5000');
});

gulp.task('start:server', function () {
    $.connect.server({
        root: [yeoman.app, '.tmp'],
        livereload: true,
        // Change this to '0.0.0.0' to access the server from outside.
        host: "0.0.0.0",
        port: 5000
    });
});

gulp.task('start:server:test', function () {
    $.connect.server({
        root: ['test', yeoman.app, '.tmp'],
        livereload: true,
        host: "0.0.0.0",
        port: 5001
    });
});

gulp.task('start:server:dnx', shell.task([
    'dnx --watch -p project.json web --server.urls http://0.0.0.0:5000'
]));

gulp.task('watch', function () {
    $.watch(paths.styles)
      .pipe($.plumber())
      .pipe(styles())
      .pipe($.connect.reload());

    $.watch(paths.views.files)
      .pipe($.plumber())
      .pipe($.connect.reload());

    $.watch(paths.scripts)
      .pipe($.plumber())
      .pipe(lintScripts())
      .pipe($.connect.reload());

    $.watch(paths.test)
      .pipe($.plumber())
      .pipe(lintScripts());

    gulp.watch('bower.json', ['bower']);
});

gulp.task('serve', function (cb) {
    runSequence('clean:tmp',
      ['lint:scripts'],
      ['start:client'],
      'watch', cb);
});

gulp.task('serve:prod', function () {
    $.connect.server({
        root: [yeoman.dist],
        livereload: true,
        port: 5000
    });
});

gulp.task('test:core', function () {
    var testToFiles = paths.testRequire.concat(paths.scripts, paths.test);
    console.info(testToFiles);
    return gulp.src(testToFiles)
      .pipe($.karma({
          configFile: paths.karma,
          autoWatch: false,
          singleRun: true,
          action: 'run'
      })).on('error', function (err) {
          // Make sure failed tests cause gulp to exit non-zero
          console.log("Failed with: ", err);
          process.exit(-1); //instead of erroring the stream, end it
      });
});

gulp.task('test', ['start:server:test', 'test:core'], function () {
    process.exit(0);
});

gulp.task('local:test', ['start:server:test'], function () {
    var testToFiles = paths.testRequire.concat(paths.scripts, paths.test);
    return gulp.src(testToFiles)
      .pipe($.karma({
          configFile: paths.karma,
          action: 'watch'
      }));
});

gulp.task('dnx:test', ['start:server:dnx', 'test:core']);

// inject bower components
gulp.task('bower', function () {
    return gulp.src(paths.views.main)
      .pipe(wiredep({
          directory: yeoman.app + '/libs',
          ignorePath: '..'
      }))
    .pipe(gulp.dest(yeoman.app + '/tpl'));
});

///////////
// Build //
///////////

gulp.task('clean:dist', function (cb) {
    rimraf('./dist', cb);
});

gulp.task('client:build', ['html', 'styles'], function () {
    var jsFilter = $.filter('**/*.js');
    var cssFilter = $.filter('**/*.css');

    return gulp.src(paths.views.main)
      .pipe($.useref({ searchPath: [yeoman.app, '.tmp'] }))
      .pipe(jsFilter)
      .pipe($.ngAnnotate())
      .pipe($.uglify())
      .pipe(jsFilter.restore())
      .pipe(cssFilter)
      .pipe($.minifyCss({ cache: true }))
      .pipe(cssFilter.restore())
      .pipe($.rev())
      .pipe($.revReplace())
      .pipe(gulp.dest(yeoman.dist));
});

gulp.task('html', function () {
    return gulp.src(yeoman.app + '/tpl/**/*')
      .pipe(gulp.dest(yeoman.dist + '/tpl'));
});

gulp.task('images', function () {
    return gulp.src(yeoman.app + '/img/**/*')
      .pipe($.cache($.imagemin({
          optimizationLevel: 5,
          progressive: true,
          interlaced: true
      })))
      .pipe(gulp.dest(yeoman.dist + '/img'));
});

gulp.task('copy:extras', function () {
    return gulp.src(yeoman.app + '/*/.*', { dot: true })
      .pipe(gulp.dest(yeoman.dist));
});

gulp.task('copy:fonts', function () {
    return gulp.src(yeoman.app + '/fonts/**/*')
      .pipe(gulp.dest(yeoman.dist + '/fonts'));
});

gulp.task('build', ['clean:dist'], function () {
    runSequence(['images', 'copy:extras', 'copy:fonts', 'client:build']);
});

gulp.task('default', ['build']);
